import json

import pymysql

from urllib import request


def get_record():
    getPersonName = "http://192.168.0.32/info/person/getPersonList/0/500";
    addName = "http://192.168.0.32/target/targetPerson/createOrUpdateTargetPerson";
    postdata = {'targetFlag': '1'}
    header = {'Content-Type': 'application/json',
              'trustToken': '93978ffb50540258b76230b654fa50d58a0bf380'}

    request_request = request.Request( getPersonName, json.dumps( postdata ).encode( 'utf-8' ), header )
    resp = request.urlopen( request_request )
    ele_json = json.loads( resp.read() )
    contents = ele_json["content"]
    for cones in contents:
        personId = cones["personId"]
        personData = {
            "targetStartTime": "2019-11-28T15:57:34.000+0800",
            "targetEndTime": "2019-12-28T15:57:34.000+0800",
            "warningConfigId": "6359e86c-e007-11e8-8f41-0242ac110002",
            "targetLevel": "18e70858-4311-11e8-99da-0242ac110003",
            "personTargetId": "bc8c69b9-11b4-11ea-b914-0242ac110003",
            "personId": personId
        }
        request_request = request.Request( addName, json.dumps( personData ).encode( 'utf-8' ), header )
        request.urlopen( request_request )


def data():
    db = pymysql.connect( "10.2.5.101", "root", "Kc@123456", "helmet_operate_sit" )
    cursor = db.cursor()
    customer_id_sql = 'SELECT customer_id FROM kc_customer_info WHERE customer_name = \'{0}\''.format(
        str( '苏州明中系统工程有限公司' ).strip() )

    cursor.execute( customer_id_sql )
    data = cursor.fetchone()
    if data == None:
        return ''
    print( data[0] )


if __name__ == '__main__':
    data()
